import socket
import struct
import yaml
class UDP_Manager:
    def __init__(self,target_ip,target_port):
        self.target_ip = target_ip
        self.target_port = target_port
        self.sockUDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  #UDP协议
            
    def Send(self,v_x,v_z):
        cmd_string = b''
        cmd_string += bytes([0xA5])
        check_sum = 0
        v_x = struct.pack('f',v_x)
        v_z = struct.pack('f',v_z)
        for i in v_x:
            cmd_string += bytes([i])
            check_sum += i
        for i in v_z:
            cmd_string += bytes([i])
            check_sum += i
        check_sum %= 256
        cmd_string += bytes([check_sum])
        self.sockUDP.sendto(cmd_string, (self.target_ip,self.target_port))
        

if __name__ == '__main__':
    f = open("config.yaml",'r',encoding='utf-8')
    result = yaml.load(f.read(),Loader=yaml.FullLoader)
    print(result['target_ip'],result['target_port'])

    v_x = float(input("v_x:"))
    v_z = float(input("v_z:"))

    a = UDP_Manager(result['target_ip'],result['target_port'])
    a.Send(v_x,v_z)

